//
//  XVCDetailMapHall.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/25.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCDetailMapHall.h"
#import "XXTool.h"
#import "XVVSelectedExhibitionHall.h"
#import "XMLMapListModel.h"

@interface XVCDetailMapHall ()<XVVSelectedExhibitionHallDelegate,UIAlertViewDelegate>

@property (nonatomic) XVVSelectedExhibitionHall *sExH;

@property (nonatomic, strong) NSMutableArray <XMLMapListModel *> *mapDataList;

@end

@implementation XVCDetailMapHall
{
    NSMutableArray  *mapTypeArray;
    
    BOOL hallUpdate;
    BOOL paroUpdate;
    NSMutableArray <XMLMapListModel *> *needUpdateMapArray;
}

-(XVVSelectedExhibitionHall*)sExH
{
    if (_sExH==nil)
    {
        _sExH = [[XVVSelectedExhibitionHall alloc] init];
        _sExH.delegate = self;
        [self.view addSubview:_sExH];
    }
    return _sExH;
}

-(void)initParameter
{
    if (_thisType==DetailMapHallCollect)
    {
        mapTypeArray = [NSMutableArray array];
        for (int i=0; i<_collectData.count; i++)
        {
            XMLCollectData *temp = _collectData[i];
            XMLMapType *temp1 = [[XMLMapType alloc] init];
            temp1.mtype = [NSString stringWithFormat:@"%d",i];
            temp1.maplistname = [NSString stringWithFormat:@"N%@",temp.tag];
            [mapTypeArray addObject:temp1];
        }
        [self.sExH setViewData:mapTypeArray];
    }
    else if(_thisType==DetailMapHallMap)
    {
        //根据服务器地图列表数据展示该页面的地图按钮
        [XMLMapListModel requestMapListWithWithMtype:_mtype completation:^(BOOL isSuccess, NSMutableArray<XMLMapListModel *> *list) {
            if (isSuccess)
            {
                //保存当前地图列表数据
                _mapDataList = list;
                mapTypeArray = [NSMutableArray array];
                for (int i = 0; i < list.count; i ++)
                {
                    XMLMapListModel *model = list[i];
                    XMLMapType *temp = [XMLMapType new];
                    temp.maplistname = model.mapname;
                    temp.mtype = [NSString stringWithFormat:@"%d",i];
                    [mapTypeArray addObject:temp];
                }
                [self.sExH setViewData:mapTypeArray];
            }
        }];
    }
}

-(void)initUI
{
    if (_thisType==DetailMapHallCollect)
    {
        [self.navigationItem setTitle:NSLocalizedString(@"我收藏的展商", nil)];
    }
    else
    {
        [self.navigationItem setTitle:NSLocalizedString(@"平面图", nil)];
    }
}

-(void)refreshLayout:(CGRect)frame
{
    self.sExH.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

#pragma mark XVVSelectedExhibitionHallDelegate
-(void)selectedExhibitionhallDidClickHallWithHallMapType:(NSInteger)type
{
    if (_thisType==DetailMapHallCollect)
    {
        XMLCollectData *temp = _collectData[type];
        NSString *hallMap = @"";
        if ([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"map%@IsUseNew",temp
                                                               .tag]])
        {
            hallMap  = [NSString stringWithFormat:@"%@/Documents/newHtml%@.html",NSHomeDirectory(),temp.tag];
        }
        else
        {
            hallMap = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"N%@",temp.tag] ofType:@"html"];
        }
        NSString *signString = @"";
        for (NSString *temp_data in temp.hallNo_array)
        {
            signString = [NSString stringWithFormat:@"%@,%@",signString,temp_data];
        }
        signString = [signString substringFromIndex:1];
        [WebController pushWebControllerWithSourceVC:self
                                       requestString:hallMap
                                             mapType:WebControllerHallShowMapUse
                                            mapTitle:[NSString stringWithFormat:@"N%@",temp.tag]
                                              signID:signString
                                             Company:@""];
    }
    else if (_thisType==DetailMapHallMap)
    {
        __block int mark = 0;
        hallUpdate = NO;
        paroUpdate = NO;
        
        needUpdateMapArray = [NSMutableArray array];
        [XMLMapListModel checkMapWithMtype:1 complete:^(BOOL isNeedUpdate, NSMutableArray<XMLMapListModel *> *updateMapArray) {
            if (isNeedUpdate)
            {
                hallUpdate = YES;
                [needUpdateMapArray addObjectsFromArray:updateMapArray];
            }
            mark ++;
            if (mark == 2)
            {
                [self isNeedShowAlertWithType:type];
            }
        }];
        
        [XMLMapListModel checkMapWithMtype:2 complete:^(BOOL isNeedUpdate, NSMutableArray<XMLMapListModel *> *updateMapArray) {
            if (isNeedUpdate)
            {
                paroUpdate = YES;
                [needUpdateMapArray addObjectsFromArray:updateMapArray];
            }
            mark ++;
            if (mark == 2)
            {
                [self isNeedShowAlertWithType:type];
            }
        }];
    }
}

-(void)isNeedShowAlertWithType:(NSInteger)type
{
    if (paroUpdate||hallUpdate)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                                                        message:NSLocalizedString(@"有新地图，是否更新", nil)
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                              otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        alert.tag = 3;
        [alert show];
    }
    else
    {
        XMLMapListModel *tapMapModel = _mapDataList[type];
        NSString * hallMap = @"";
        if (_mtype == 1)
        {
            hallMap = [NSString stringWithFormat:@"%@/Documents/%ld%@.html",NSHomeDirectory(),_mtype,tapMapModel.mapname];
        }
        else
        {
            hallMap = [NSString stringWithFormat:@"%@/Documents/%ld%@.png",NSHomeDirectory(),_mtype,tapMapModel.mapname];
        }
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:hallMap])
        {
            [WebController pushWebControllerWithSourceVC:self
                                           requestString:hallMap
                                                 mapType:WebControllerHallShowMapUse
                                                mapTitle:tapMapModel.mapname
                                                  signID:@""
                                                 Company:@""];
        }
    }
}

#pragma mark UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==3)
    {
        if (buttonIndex==1)
        {
            NSMutableArray <XMLXXDownFileModel*>* downfileArray = [NSMutableArray array];
            if (needUpdateMapArray.count > 0)
            {
                for (int i = 0; i < needUpdateMapArray.count; i ++)
                {
                    XMLMapListModel *temp = needUpdateMapArray[i];
                    XMLXXDownFileModel *img = [XMLXXDownFileModel newXXDownFileModelWithUrl:temp.mappath saveName:[NSString stringWithFormat:@"%@%@.png",temp.mtype,temp.mapname] saveType:IMG];
                    [downfileArray addObject:img];
                    
                    if ([temp.mtype integerValue] == 1)
                    {
                        XMLXXDownFileModel *html = [XMLXXDownFileModel newXXDownFileModelWithUrl:temp.mapphtml saveName:[NSString stringWithFormat:@"%@%@.html",temp.mtype,temp.mapname] saveType:HTML];
                        [downfileArray addObject:html];
                    }
                }
            }
            
            UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle:nil
                                                                    message:NSLocalizedString(@"正在下载", nil)
                                                                   delegate:self
                                                          cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                                          otherButtonTitles:nil, nil];
            [progressAlert showWithProgress];
            [progressAlert show];
            
            [XXDownFileToFoler
             downFileWithUrlArray:downfileArray
             downProgressBlock:^(float rate) {
                 [progressAlert setProgressWithRate:rate];
             } completeBlock:^(BOOL isSuccess) {
                 [progressAlert dismissWithClickedButtonIndex:0 animated:NO];
                 [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"地图更新成功", nil)];
                 //地图更新成功，更新地图对应版本号
                 NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                 for (int i = 0; i < needUpdateMapArray.count; i ++)
                 {
                     XMLMapListModel *temp = needUpdateMapArray[i];
                     [user setInteger:[temp.mapversion integerValue] forKey:[NSString stringWithFormat:@"%@%@%@",temp.mtype,mtypeMapVision,temp.mapname]];
                 }
                 [user synchronize];
             }];
        }
    }
}

#pragma mark api
+(void)pushDetailMapHallVCWithSourceVC:(UIViewController *)sourceVC
                              HallType:(DetailMapHallType)type
                                 mtype:(NSInteger)mtype
                           collectData:(NSMutableArray *)collect
{
    XVCDetailMapHall* vc = [[XVCDetailMapHall alloc] init];
    vc.thisType = type;
    vc.mtype = mtype;
    vc.collectData = collect;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
